package org.komarichin.jdo.dao;

import org.komarichin.beans.dao.IPartitionDAO;
import org.komarichin.jdo.persistence.Partition;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * User: Mykhaylo Komarichyn
 * Date: 16.12.2009
 * Time: 15:03:56
 */
@Transactional(readOnly = true)
public class PartitionDAO extends JdoGenericLanguageDAOSupport<Partition, Long> implements IPartitionDAO<Partition, Long> {
    private static final long serialVersionUID = 705528504338353781L;
    private static final Logger log = LoggerFactory.getLogger(PartitionDAO.class);

    protected PartitionDAO(Class<Partition> type) {
        super(type);
    }

    @Override
    public Partition save(Partition entity) {
        entity.setCreate(new Date());
        return super.save(entity);
    }

    @Override
    @Transactional(readOnly = false)
    public Long saveOrUpdate(Partition entity) {
        log.info("save or update entity: {}", entity);
        Partition p = getJdoTemplate().getObjectById(getType(), entity.getId());
        if (p == null) {
            return save(p).getId();
        } else {
            p.setContent(entity.getContent());
            p.setDescription(entity.getDescription());
            p.setKeyWords(entity.getKeyWords());
            p.setLanguage(entity.getLanguage());
            p.setName(entity.getName());
            p.setTitle(entity.getTitle());
            p.setUpdate();
            return p.getId();
        }
    }

    @Override
    public Partition newInstance() {
        log.debug("create new instance partition");
        return new Partition();
    }
}
